/*
 * $Id: om-Container.js,v 1.14 2012/03/15 06:14:47 linxiaomin Exp $
 * operamasks-ui omProgressbar @VERSION
 *
 * Copyright 2012, AUTHORS.txt (http://ui.operamasks.org/about)
 * Dual licensed under the MIT or LGPL Version 2 licenses.
 * http://ui.operamasks.org/license
 *
 * http://ui.operamasks.org/docs/
 *
 * Depends:
 *  om-core.js
 */
/** 
 * @name omContainer
 * @class 能够自动计算高度的容器。<br/>
 *<div id="searchbox"></div><div id="buttonbar"></div><div id=container1>omGrid表格</div>
 *searchbox,buttonbar一般来是固定高度，而container1中的表格根据内容而定，omGrid的高度设为fit
 * <b>示例：</b><br/>
 * <pre>
 * &lt;script type="text/javascript" &gt;
 * $(document).ready(function() {
 *     $('#selector').omContainer({fit:true});
 * });
 * &lt;/script&gt;
 * 
 * &lt;div id="selector" /&gt;
 * </pre>
 * @constructor
 * @description 构造函数. 
 * @param p 标准config对象：{}
 * @example
 * $('#selector').omContainer();
 */
;(function($) {
	
$.omWidget("om.omContainer", {
		
	options: /**@lends omProgressbar#*/{
		/**
         * 设置容器的高度,单位为像素。
         * @type Number,String
         * @default 400
         * @example
         * $("#selector").omContainer({height: 500});
         */
        height: 400,
			/**
         * 设置容器的宽度,单位为像素。
         * @type Number,String
         * @default 400
         * @example
         * $("#selector").omContainer({height: 500});
         */
        width: 400,
			/**
         * 设置容器属性,容器是固定的还是根上级自动计算高度。
         * @type boolean
         * @default false
         * @example
         * $("#selector").omContainer({fit: true});
         */
		fit: false
		
	},
	/**
     * 获取容器的高度。没有传入参数时，该方法为获取当前进度值，反之，则设置当前进度值。
     * @name omContainer#value
     * @param 无
     * @function
     * @example
     * $("#selector").omContainer('value');
     * 
     */
	value:function(){
		return this.element.height();
	},
	_create: function() {
	    var $ele = this.element;
		$ele.addClass( "om-widget om-fit" );
	},
	
	_init : function() {
		var $ele = this.element;
		if (this.options.fit)
		{
			this.options.height=this._caclHeight($ele);
		}
		$ele.css('height',this.options.height);
	},
	_caclHeight:function(elem) {
			//if (!this.canLayout()) return;检查是否可以输出
			var pare = elem[0].parentNode,
	        child =this._getChildNodes(pare);
		   // if (pare == document.body) elem[0].style.height = "0px";
			var pareHeight =this._getElemHeight(pare, true);
	        for (var E = 0,  D = child.length; E < D; E++) {
					var C = child[E];
		            if (C == elem[0]) continue;
		            var G = this._Jig(C, "position");
				    if (G == "absolute" || G == "fixed") continue;
		            var A = this._getElemHeight(C),
				    I = this._getMarginPosition(C);
		            pareHeight = pareHeight - A - I.top - I.bottom
		        }
				var H = this._getBorderWidth(elem[0]),
		        B = this._getPaddingPosition(elem[0]),
		        I = this._getMarginPosition(elem[0]);
		        pareHeight = pareHeight - I.top - I.bottom;
				if (jQuery.boxModel) pareHeight = pareHeight - B.top - B.bottom - H.top - H.bottom;
		        if (pareHeight < 0) pareHeight = 0;
				//elem[0].style.height = pareHeight + "px";
			return pareHeight;
    },
	_getElemWidth :function(elem, isOuter) {
	    elem = this._getDomElem(elem);
		if (elem.style.display == "none" || elem.type == "text/javascript") return 0;
	    return isOuter ? jQuery(elem).width() : jQuery(elem).outerWidth()
	},
	 _getElemHeight :function(elem, isOuter) {
	    elem = this._getDomElem(elem);
		if (elem.style.display == "none" || elem.type == "text/javascript") return 0;
	    return isOuter ? jQuery(elem).height() : jQuery(elem).outerHeight();
	},
	_getMarginPosition : function(elem) {
	    elem = this._getDomElem(elem);
		var $elem = jQuery(elem);
	    return {
		    top: parseInt($elem.css("margin-top"), 10) || 0,
			left: parseInt($elem.css("margin-left"), 10) || 0,
	        bottom: parseInt($elem.css("margin-bottom"), 10) || 0,
		    right: parseInt($elem.css("margin-right"), 10) || 0
	    }
	},
	_getBorderWidth : function(elem) {
	    elem = this._getDomElem(elem);
		var $elem = jQuery(elem);
	    return {
		    top: parseInt($elem.css("border-top-width"), 10) || 0,
			left: parseInt($elem.css("border-left-width"), 10) || 0,
	        bottom: parseInt($elem.css("border-bottom-width"), 10) || 0,
		    right: parseInt($elem.css("border-right-width"), 10) || 0
	    }
	},
	_getPaddingPosition : function(elem) {
		elem =this._getDomElem(elem);
	    var $elem = jQuery(elem);
		return {
			top: parseInt($elem.css("padding-top"), 10) || 0,
	        left: parseInt($elem.css("padding-left"), 10) || 0,
		    bottom: parseInt($elem.css("padding-bottom"), 10) || 0,
			right: parseInt($elem.css("padding-right"), 10) || 0
	    }
	},
	_getDomElem : function(s) {
		if (typeof s == "string") {
			if (s.charAt(0) == "#") s = $.substr(1);
	        return document.getElementById(s)
		} else return s
	},
	_Jig : function() {
		var d = document.defaultView;
	    return new Function("el", "style", ["style.indexOf('-')>-1 && (style=style.replace(/-(\\w)/g,function(m,a){return a.toUpperCase()}));", "style=='float' && (style='", d ? "cssFloat": "styleFloat", "');return el.style[style] || ", d ? "window.getComputedStyle(el,null)[style]": "el.currentStyle[style]", " || null;"].join(""))
	},
	_getChildNodes: function(A, C) {
        A = this._getDomElem(A);
        if (!A) return;
        var E = A.childNodes,
        B = [];
        for (var i = 0,
        D = E.length; i < D; i++) {
            var _ = E[i];
            if (_.nodeType == 1 || C === true) B.push(_)
        }
        return B
	}
});

})(jQuery);